<?php

/**
 * 
 */
class Main extends CI_Controller {

    var $abc = 0;
    var $def = 0;
    var $count = 0;
    var $rou = 0;

    function __construct() {
        parent::__construct();

        $this->load->library('googlemaps');
        $this->load->library('Jquery_ext');
        if ($this->session->userdata('sess_id') == FALSE) {
   
        }
        $this->load->model(array(
            'comments_model',
            'reports_model',
            'routes_model',
            'thumbs_model',
            'users_model',
            'login_model',
            'register_model')
        );
        $data['lastReportId'] = $this->reports_model->fetchLastReportId();
        $id = $data['lastReportId']['report_id'];
        $this->session->set_userdata('last_report_id', $id);
    }

    public function index() {
        $data = array('reports' => $this->reports_model->fetchReports(),
            'routes' => $this->routes_model->fetchRoutes(),
            'thumbs' => $this->thumbs_model->fetchThumbs(),
            'users' => $this->users_model->fetchUsers(),
            'lastUser' => $this->users_model->fetchLastUser(),
            'count_reports' => $this->reports_model->getAllReports(),
            'count_reports_all' => $this->reports_model->countAllReports(),
            'usersCount' => $this->users_model->fetchUsersCount());


        $basicRouteStats = $this->routes_model->fetchBasicRoutesStats();
        $allRoutes = $this->routes_model->fetchRoutes();

        foreach ($allRoutes as $key) {
            $plus = $this->rou++;
            $from = $key['s_street'] . ", " . $key['s_city'];
            $data['pos_from'][$plus] = rtrim($from, ', ') . "";
            $data['s_state'][$plus] = $key['s_state'];
            $data['s_image'][$plus] = $this->stateToImage($key['s_state']);

            $to = $key['e_street'] . ", " . $key['e_city'];
            $data['pos_to'][$plus] = rtrim($to, ', ') . "";
            $data['e_state'][$plus] = $key['e_state'];
            $data['e_image'][$plus] = $this->stateToImage($key['e_state']);
            $data['route_km'][$plus] = $key['kilometres'];
            $data['radars'][$plus] = $key['n_police'];
            $data['patrols'][$plus] = $key['n_patrol'];
            $data['accidents'][$plus] = $key['n_accident'];
            $data['mobileradars'][$plus] = $key['n_mobileradar'];
        }
        $data['all_time_driven'] = $this->parseMinutesToAny($basicRouteStats[0]['td']);
        $data['all_kilometres'] = number_format($basicRouteStats[0]['km'], 0, '', ' ');
        $data['all_reports'] = number_format($basicRouteStats[0]['rep'], 0, '', ' ');


        $data['lastReports'] = $this->reports_model->fetchLast25();

        foreach ($data['lastReports'] as $key) {
            $plus = $this->abc++;
            $data['report_id'][$plus] = $key['report_id'];
            $data['obr'][$plus] = $this->getImage($key['type_id']);
            $data['date'][$plus] = $this->parseDate($key['seen']);
            $data['time'][$plus] = $this->parseTime($key['seen']);
            $data['city'][$plus] = $this->removeZipcode($key['city']);
            $data['lat'][$plus] = $key['latitude'];
            $data['reported'][$plus] = $key['reported'];
            $data['lng'][$plus] = $key['longitude'];
            $data['city_'][$plus] = $key['city_noaccent'];
            $data['street_'][$plus] = $key['street_noaccent'];
            $data['area_id'][$plus] = $key['area_id'];
        }

        $config['center'] = '48.75,19.70';
        $config['map_height'] = 350;
        $config['sensor'] = false;
        $config['map_width'] = 624;
        $config['zoom'] = '7';
        $this->googlemaps->initialize($config);
        $data['map'] = $this->googlemaps->create_map();


        $this->load->view('templates/header', $data);
        $this->load->view('templates/content', $data);
        $this->load->view('templates/footer');
    }

    public function doMapy($type) { /////// json.php
        $data['json'] = $this->reports_model->fetchtoJson($type);

        foreach ($data['json'] as $key) {
            $plus = $this->def++;
            $data['report_id'][$plus] = $key['report_id'];
            $data['obr'][$plus] = $this->getImage($key['type_id']);
            $data['date'][$plus] = $this->parseDate($key['seen']);
            $data['time'][$plus] = $this->parseTime($key['seen']);
            $data['city'][$plus] = $this->removeZipcode($key['city']);
            $data['city_'][$plus] = $key['city_noaccent'];
            $data['street_'][$plus] = $key['street_noaccent'];
            $data['lat'][$plus] = $key['latitude'];
            $data['lon'][$plus] = $key['longitude'];

            $marker = array();
            $marker['position'] = $data['lat'][$plus] . "," . $data['lon'][$plus];
            $marker['draggable'] = TRUE;
            $marker['animation'] = 'DROP';
            $this->googlemaps->add_marker($marker);
        }

        $config['center'] = '48.75,19.70';
        $config['map_height'] = 350;
        $config['map_width'] = 624;
        $config['sensor'] = false;
        $config['cluster'] = TRUE;
        $config['zoom'] = '7';
        $this->googlemaps->initialize($config);


        $this->load->view('reports/json', $data);
        $data['map'] = $this->googlemaps->create_map();
    }

    public function doGrafu($date) {
        $data['toGraph'] = $this->reports_model->fetchToGraph($date);
        // $data['obr'] = $this->getImage($type);
        $this->load->view('reports/graph', $data);
    }

    public function userCount() {
        $data['usersCount'] = $this->users_model->fetchUsersCount();
        $this->load->view('users/getUsers', $data);
    }

    public function fetchBasicRoutesStats() {
        $data['basicRouteStats'] = $this->routes_model->fetchBasicRoutesStats();
        $this->load->view('templates/content', $data);
    }

    private function getImage($argument) {
        switch ($argument) {
            case 1:
                $image = "speed.png";
                break;
            case 2:
                $image = "police.png";
                break;
            case 3:
                $image = "accident.png";
                break;
            case 4:
                $image = "mobile_radar.png";
                break;
        }

        return $image;
    }

    private function parseDate($value) {
        $date = substr($value, 0, 10);
        $hour = substr($value, 11, 2);
        $now = date("Y-m-d H:i:s", time());

        if (date("Y-m-d", strtotime("$now -0 days")) == $date) {
            $when = "Dnes";
        } else if (date("Y-m-d", strtotime("$now -1 days")) == $date) {
            $when = "Včera";
        } else if (date("Y-m-d", strtotime("$now -2 days")) == $date) {
            $when = "Predvčerom";
        } else {
            $when = date("d.m.", strtotime($value));
        }
        return $when;
    }

    private function parseTime($value) {
        return date(" H:i", strtotime($value));
    }

    private function removeZipcode($city) {
        return preg_replace("/[0-9]/", "", $city);
    }

    public function login() {
        if (!$this->login_model->doLogin()) {
            echo json_encode("Error");
        } else {
            $this->login_model->doLogin();
            echo json_encode("LoginOk");
        }
    }

    public function logout() {
        $this->login_model->doLogout();
    }

    private function stripAccents($str) {
        $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 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 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
        $b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
        return str_replace($a, $b, $str);
    }

    private function parseMinutesToAny($minutes) {
        $arr = array();
        $years = floor($minutes / (24 * 60 * 365));
        $days = floor($minutes / (24 * 60)) - ($years * (365));
        $hours = floor($minutes / (60)) - (($years * (24 * 365)) + ($days) * 24);

        $arr['years'] = $years;
        $arr['days'] = $days;
        $arr['hours'] = $hours;
        return $arr;
    }

    private function stateToImage($state) {


// $arr = array(array('Slovenská republika' => 'sk.png'),
// 				     array('Česká republika' => 'cz.png'));
        $obr = "";
        switch ($state) {


            case 'Slovakia':
                $obr = 'sk.png';
                break;
            case 'Slovenská republika':
                $obr = 'sk.png';
                break;

            case 'Česká republika':
                $obr = 'cz.png';
                break;

            case 'Nemecko':
                $obr = 'de.png';
                break;

            case 'Rakúsko':
                $obr = 'austria.jpg';
                break;

            case 'Francúzsko':
                $obr = 'france.jpg';
                break;

            case 'Maďarsko':
                $obr = 'hungary.gif';
                break;

            case 'Poľsko':
                $obr = 'pol.gif';
                break;

            case 'Taliansko':
                $obr = 'italy.jpg';
                break;

            case 'Portugalsko':
                $obr = 'port.jpg';
                break;

            case 'Rumunsko':
                $obr = 'romania.jpg';
                break;

            case 'Srbsko':
                $obr = 'srbsko.jpg';
                break;

            case 'Španielsko':
                $obr = 'spain.jpg';
                break;

            case 'Luxembursko':
                $obr = 'lux.jpg';
                break;

            case 'Rusko':
                $obr = 'russia.jpg';
                break;

            case 'Švajčiarsko':
                $obr = 'switz.jpg';
                break;

            case 'Ukrajina':
                $obr = 'ukraine.gif';
                break;

            default:
                $obr = 'none.png';
                break;
        }
        return $obr;
    }

}

?>